import {
  ADDCART,
  CHANGEBUY,
  DELETESHOP,
  CHANGEISIN,
  CREATEORDER,
  ORDERLISTEND,
  ADDADDRESS,
  EDITADDRESS,
  DELADDRESS,
} from "./mutation-types";
export default {
  [ADDCART](state, val) {
    state.cartList = val;
  },
  // 点击编辑，让下方的全选和删除显示
  [CHANGEBUY](state) {
    state.isBuy = !state.isBuy;
  },
  // 点击删除
  [DELETESHOP](state, value) {
    state.cartList = value;
  },
  // 购物车头部右侧‘编辑’与‘完成’文字的切换
  [CHANGEISIN](state) {
    state.isIn = !state.isIn;
    if (state.cartList.length === 0) {
      state.isIn = false;
    }
  },
  [CREATEORDER](state, value) {
    state.orderList = value;
  },
  [ORDERLISTEND](state) {
    state.orderListEnd = state.orderListEnd.concat(state.orderList);
  },
  [ADDADDRESS](state, value) {
    // 判断一下新增的地址是不是为默认地址
    state.userAddress.map((item) => {
      if (value.isDefault) {
        item.isDefault = false;
      }
    });
    state.userAddress.push(value);
  },
  [EDITADDRESS](state, value) {
    state.userAddress = state.userAddress.map((item) => {
      if (value.isDefault) {
        item.isDefault = false;
      }
      return value.id === item.id ? value : item;
    });
  },
  [DELADDRESS](state, value) {
    state.userAddress = state.userAddress.filter(
      (item) => !(item.id === value.id)
    );
    if (value.isDefault) {
      state.userAddress[0].isDefault = true;
    }
  },
};
